package com.dxt.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dxt.common.*;
import com.dxt.message.AppRequestMessage;
import com.dxt.message.ReponseMessage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Service("checkLoginService")
public class CheckLoginService extends IBaseBusiService {
    private static final Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[1].getClassName());

    @Autowired
    CacheManager cacheManager;

    @Override
//    @Transactional
    public ReponseMessage execute(String reqInfo, String url) {
        logger.debug(LogHelper._FUNC_START_());
        ReponseMessage message = new ReponseMessage();
        AppRequestMessage msg = getMsg();
        JSONObject jsonObject = JSON.parseObject(reqInfo);
        try {
            /**
             * 校验必填业务参数是否为空
             */
            checkParams(jsonObject, AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_USERNAME,
                    AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_PASSWORD,
                    AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_SOURCE);
        } catch (MyBusiException e) {
            message.setMsg(AppConstant.REPONSE_CODE.BUSI_ERROR, AppConstant.REPONSE_MSG.SYS_PARAM_ERROR_MSG,
                    e.getMessage());
            return message;
        }
        String response = null;
        HttpUtil httpUtil = new HttpUtil();
        try {
            response = httpUtil.getHttpPostResponse(url, "json", reqInfo,
                    cacheManager.getSysConfigByCode(AppConstant.SYS_CONFIG_KEY.KEY_TOKEN));
        } catch (IOException e) {
            e.printStackTrace();
        }
        logger.info("response="+response);
        JSONObject jsonObj = null;

        if(StringUtils.isNotBlank(response)){
            jsonObj = JSON.parseObject(response);
        }
        JSONObject retObject = new JSONObject();
        if(jsonObj != null) {
            try {
                String code = jsonObj.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_OUT_CODE);
                String respMessage = java.net.URLDecoder.decode(jsonObj.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_OUT_MESSAGE), "UTF-8");
                Boolean success = jsonObj.getBoolean(AppConstant.REQUEST_REPONSE_PARAM.PARAM_OUT_SUCCESS);
                Long timestamp = jsonObj.getLong(AppConstant.REQUEST_REPONSE_PARAM.PARAM_OUT_TIMESTAMP);
                if(StringUtils.isNotBlank(code) && AppConstant.REPONSE_CODE.SUCCESS_CODE.equals(code)){
                    String result = jsonObj.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_OUT_RESULT);
                    retObject.put(AppConstant.REQUEST_REPONSE_PARAM.PARAM_OUT_RESULT, result);
                    message.setMsg(code,AppConstant.REPONSE_MSG.SYS_REQUEST_OK_MSG, retObject, success, timestamp);
                }else{
                    message.setMsg(code,respMessage,success,timestamp);
                }
            }catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
            }
        }
        return message;
    }

}
